import {contextBridge, ipcRenderer} from 'electron'

contextBridge.exposeInMainWorld('electron', {
    // MySQL 数据库操作
    mysql: {
        // 创建连接池
        createPool: async (poolId, config) => {
            return await ipcRenderer.invoke('mysql-create-pool', poolId, config)
        },

        // 执行查询
        query: async (poolId, sql, params = []) => {
            return await ipcRenderer.invoke('mysql-query', poolId, sql, params)
        },

        // 批量查询
        batchQuery: async (poolId, queries) => {
            return await ipcRenderer.invoke('mysql-batch-query', poolId, queries)
        },

        // 事务操作
        transaction: async (poolId, queries) => {
            return await ipcRenderer.invoke('mysql-transaction', poolId, queries)
        },

        // 关闭连接池
        closePool: async (poolId) => {
            return await ipcRenderer.invoke('mysql-close-pool', poolId)
        },

        // 获取连接池状态
        getPoolStatus: async (poolId) => {
            return await ipcRenderer.invoke('mysql-pool-status', poolId)
        }
    },

    // SQLite 数据库操作
    sqlite: {
        // 创建连接
        createConnection: async (dbId, config) => {
            return await ipcRenderer.invoke('sqlite-create-connection', dbId, config)
        },

        // 执行查询
        query: async (dbId, sql, params = [], single = false) => {
            return await ipcRenderer.invoke('sqlite-query', dbId, sql, params, single)
        },

        // 执行操作（INSERT, UPDATE, DELETE）
        run: async (dbId, sql, params = []) => {
            return await ipcRenderer.invoke('sqlite-run', dbId, sql, params)
        },

        // 批量查询
        batchQuery: async (dbId, queries) => {
            return await ipcRenderer.invoke('sqlite-batch-query', dbId, queries)
        },

        // 事务操作
        transaction: async (dbId, queries) => {
            return await ipcRenderer.invoke('sqlite-transaction', dbId, queries)
        },

        // 执行脚本
        script: async (dbId, script) => {
            return await ipcRenderer.invoke('sqlite-script', dbId, script)
        },

        // 获取表信息
        getTableInfo: async (dbId, tableName = null) => {
            return await ipcRenderer.invoke('sqlite-table-info', dbId, tableName)
        },

        // 优化数据库
        optimize: async (dbId) => {
            return await ipcRenderer.invoke('sqlite-optimize', dbId)
        },

        // 备份数据库
        backup: async (dbId, backupPath) => {
            return await ipcRenderer.invoke('sqlite-backup', dbId, backupPath)
        },

        // 关闭连接
        closeConnection: async (dbId) => {
            return await ipcRenderer.invoke('sqlite-close-connection', dbId)
        },

        // 获取连接状态
        getConnectionStatus: async (dbId) => {
            return await ipcRenderer.invoke('sqlite-connection-status', dbId)
        }
    },

    // 系统db操作
    systemDb: async (sql, params = []) => {
        return await ipcRenderer.invoke('systemDb', {sql, params})  // 一定要是对象
    },
    ping: async (ip) => {
        return await ipcRenderer.invoke('ping', ip)
    }
})
